
_realname=krita
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.2.2
pkgrel=10
pkgdesc="A professional FREE and open source painting program (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://krita.org/"
license=("spdx:GPL-3.0-only")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-eigen3"
  "${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
  "${MINGW_PACKAGE_PREFIX}-immer"
  "${MINGW_PACKAGE_PREFIX}-kdoctools-qt5"
  "${MINGW_PACKAGE_PREFIX}-kitemmodels-qt5"
  "${MINGW_PACKAGE_PREFIX}-kseexpr"
  "${MINGW_PACKAGE_PREFIX}-lager"
  "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
  "${MINGW_PACKAGE_PREFIX}-qt5-tools"
  "${MINGW_PACKAGE_PREFIX}-sip"
  "${MINGW_PACKAGE_PREFIX}-xsimd"
  "${MINGW_PACKAGE_PREFIX}-zug"
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-angleproject"
  "${MINGW_PACKAGE_PREFIX}-exiv2"
  "${MINGW_PACKAGE_PREFIX}-fftw"
  "${MINGW_PACKAGE_PREFIX}-fontconfig"
  "${MINGW_PACKAGE_PREFIX}-freetype"
  "${MINGW_PACKAGE_PREFIX}-fribidi"
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-giflib"
  "${MINGW_PACKAGE_PREFIX}-gsl"
  "${MINGW_PACKAGE_PREFIX}-harfbuzz"
  "${MINGW_PACKAGE_PREFIX}-icoutils"
  "${MINGW_PACKAGE_PREFIX}-imath"
  "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
  "${MINGW_PACKAGE_PREFIX}-kcompletion-qt5"
  "${MINGW_PACKAGE_PREFIX}-kconfig-qt5"
  "${MINGW_PACKAGE_PREFIX}-kcoreaddons-qt5"
  "${MINGW_PACKAGE_PREFIX}-kguiaddons-qt5"
  "${MINGW_PACKAGE_PREFIX}-ki18n-qt5"
  "${MINGW_PACKAGE_PREFIX}-kitemviews-qt5"
  "${MINGW_PACKAGE_PREFIX}-kwidgetsaddons-qt5"
  "${MINGW_PACKAGE_PREFIX}-kwindowsystem-qt5"
  "${MINGW_PACKAGE_PREFIX}-lcms2"
  "${MINGW_PACKAGE_PREFIX}-libheif"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-libjxl"
  "${MINGW_PACKAGE_PREFIX}-libmypaint"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  #"${MINGW_PACKAGE_PREFIX}-libraw" KF5KDcraw
  "${MINGW_PACKAGE_PREFIX}-libtiff"
  "${MINGW_PACKAGE_PREFIX}-libunibreak"
  "${MINGW_PACKAGE_PREFIX}-libwebp"
  "${MINGW_PACKAGE_PREFIX}-mlt"
  "${MINGW_PACKAGE_PREFIX}-opencolorio"
  "${MINGW_PACKAGE_PREFIX}-openexr"
  "${MINGW_PACKAGE_PREFIX}-openjpeg2"
  "${MINGW_PACKAGE_PREFIX}-poppler-qt5"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-qt5-base"
  "${MINGW_PACKAGE_PREFIX}-qt5-svg"
  "${MINGW_PACKAGE_PREFIX}-quazip-qt5"
  "${MINGW_PACKAGE_PREFIX}-zlib"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-ffmpeg: Support for saving animations"
  "${MINGW_PACKAGE_PREFIX}-kimageformats-qt5: Support for PSD files"
  "${MINGW_PACKAGE_PREFIX}-kseexpr: SeExpr generator layer"
)
source=("https://download.kde.org/stable/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz"
        001-mingw-interface-conflict.patch
        002-exiv2-missing-wpath.patch
        004-qt5-qguiapplication.patch
        005-no-installer.patch
        sip-6.8.patch
        xsimd-12.patch
        https://invent.kde.org/graphics/krita/-/commit/ace7edcca6ad322581ab39620f21ccf3ffbd3b5a.patch)
sha256sums=('41515784d65b5bf12919df909f7406dc90f37076587b8c459ef2abd569a71adb'
            'a5680deef222fa56f0c2b7b4849ad82b831f4afc23b0fd4b5ba4944a6855ca1c'
            'db4bb1591015d1b4cca030bf9beaf58c6335958e3986b78444bf350d71bf18aa'
            'd8246397d93008fe55bdcddd409baaabfffbe95d0bea05a48a7584a7a60e48b9'
            'd80a2728692a1456b133a17e33a0083adab8c96270b4d6e94da07ed8141c4503'
            'bb3b503993030bb98a99c6a0376c65ee74d0c32c1e8932110698682eed1e3d3c'
            'bdf2fd09b65dfae8fd7817b302486d6b82df8c0d56fd980afac019a82d3cd716'
            'da53f913276982ada75cc4dac2e714ab90588b1d0b5bd94905eebe231e6072d6')
noextract=("krita-${pkgver}.tar.xz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  echo "Extracting krita-${pkgver}.tar.xz ..."
  tar -xJf krita-${pkgver}.tar.xz || true

  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    001-mingw-interface-conflict.patch \
    002-exiv2-missing-wpath.patch \
    005-no-installer.patch

  # Workaround for https://bugreports.qt.io/browse/QTBUG-111527
  apply_patch_with_msg \
    004-qt5-qguiapplication.patch

  # https://bugs.kde.org/show_bug.cgi?id=478987
  apply_patch_with_msg \
    ace7edcca6ad322581ab39620f21ccf3ffbd3b5a.patch

  # From Arch
  apply_patch_with_msg \
    sip-6.8.patch \
    xsimd-12.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -GNinja \
    "${_extra_config[@]}" \
    -DBUILD_TESTING="OFF" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -vDm 644 "${srcdir}"/${_realname}-${pkgver}/COPYING -t "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/"
  install -vDm 644 "${srcdir}"/${_realname}-${pkgver}/{AUTHORS,README.md} -t "${pkgdir}${MINGW_PREFIX}/share/doc/$_realname/"
}
